package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreLanguage;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.LanguageForm;
import com.edu.hre.struts.form.admin.LanguageListForm;

public class LanguageDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "languageDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<LanguageForm> getLanguageListInfo(int limit, int offset){

		List<HreLanguage> hreLanguages = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreLanguage.class);
		ArrayList<LanguageForm> languages = new ArrayList<LanguageForm>();
		
		if (hreLanguages == null) {
			return languages;
		}
		for (HreLanguage hreLang: hreLanguages) {
			
			try {

				LanguageForm language = new LanguageForm();
				language.setId(hreLang.getId() + "");
				language.setName(CommonUtils.normalizeString(hreLang.getName()));
				
				languages.add(language);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return languages;
	}
	
	public int countLanguages(){

		int count = baseService.countByColumns(null, null, null, "id", HreLanguage.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLanguages(LanguageListForm form){

		ArrayList<LanguageForm> info = (ArrayList<LanguageForm>) form.getAllLanguages();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {
				LanguageForm lang = info.get(i);
				if (lang.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(lang.getId()), null, null, 0, HreLanguage.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
